package com.iwunu.gateway.util;

/**
 * @author kin
 * @date 2019/6/18 2:28 PM
 * @description 蓝牙16进制数据组装类
 */
public class BluetoothDataUtil {

    private static final String selectBindLockStatus="AAAAC00014" ;
//    private static final String selectPower="AAAAA400F8" ;
    private static final String selectPower="AAAAA40000" ;
    private static final String readRecords="AAAA28007C" ;
    private static final String readRecordsCount="AAAA2A007E" ;
    private static final String clearCardNoData="AAAA2E0082" ;
    private static final String readCardUid="AAAA300084" ;
    private static final String readNbImei="AAAA320086" ;
    /**O版本查询记录的时候参数不同地方**/
    private static final String readRecords_o="AAAA70007C" ;
    private static final String readRecordsCount_o="AAAA72007E" ;
    private static final String clearCardNoData_o="AAAA740082" ;
    /**
     * 2.1    查询绑定状态
     * @return
     */
    public static String selectBindLockStatus(){
        return selectBindLockStatus;
    }

    /**
     * 2.2    查询电量状态
     * @return
     */
    public static String selectPower(){
        return selectPower;
    }

    /**
     * 3.7  开锁Step1
     * @param data
     * @param random
     * @return
     */
    public static String openLockStep1(int data,int random){
        StringBuffer buffer = new StringBuffer();
        buffer.append("AAAAEC").append("08").
                append(
                        ""
                ).append(makeChecksum(buffer.toString()));
        return buffer.toString();
    }


    /**
     * 4.3  删除卡
     * @param baseCode
     * @param cardNo
     * @return
     */
    public static String delCardNo(int baseCode,Long cardNo){
        StringBuffer buffer = new StringBuffer("AAAA1608");
        return buffer.toString() ;
    }



    /**
     * 5.2  读记录
     * @return
     */
    public static String readRecords(String type){
//        StringBuffer buffer = new StringBuffer();
//        buffer.append("AAAA28").append("00").
//                append(makeChecksum(buffer.toString()));
//        return buffer.toString() ;
    	if(type.toUpperCase().equals("N")||type.equals("U")) {
    		return readRecords;
    	}else {
    		return readRecords_o;
    	}
    }

    /**
     * 5.3  读记录条数
     * @return
     */
    public static String readRecordsCount(String type){
      	if(type.toUpperCase().equals("N")||type.equals("U")) {
    		return readRecordsCount;
    	}else {
    		return readRecordsCount_o;
    	}
    }


    /**
     * 6.2发卡器- 清除卡的数据
     * @return
     */
    public static String clearCardNoData(String type) {
      	if(type.toUpperCase().equals("N")||type.equals("U")) {
    		return clearCardNoData;
    	}else {
    		return clearCardNoData_o;
    	}
    }

    /**
     * 6.3  发卡器-读卡UID
     * @return
     */
    public static String readCardUid(){
        return readCardUid;
    }

    /**
     * 7.1  读NB的IMEI
     * @return
     */
    public static String readNbImei(){
//        StringBuffer buffer = new StringBuffer();
//        buffer.append("AAAA32").append("00").
//                append(makeChecksum(buffer.toString()));
//        return buffer.toString();
        return readNbImei;
    }


    /**
     ** 计算数据校验和
     * @param data
     * @return
     */
    public static String makeChecksum(String data) {
        int total = 0;
        int len = data.length();
        int num = 0;
        while (num < len) {
            String s = data.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        /**
         * *用256求余最大是255，即16进制的FF
         */
        int mod = total % 256;
        String hex = Integer.toHexString(mod);
        len = hex.length();
        // 如果不够校验位的长度，补0,这里用的是两位校验
        if (len < 2) {
            hex = "0" + hex;
        }
        return hex.toUpperCase();
    }

    public static long getUint32(long l){
        return l & 0x00000000ffffffff;
    }



    private static Integer macToInt(String mac){
        String macNew = mac.replace(":", "").substring(4);
        return Integer.parseInt(macNew,16);
    }
    public static String bytesToHexString(byte[] bArr) {
        StringBuffer sb = new StringBuffer(bArr.length);
        String sTmp;
        for (int i = 0; i < bArr.length; i++) {
            sTmp = Integer.toHexString(0xFF & bArr[i]);
            if (sTmp.length() < 2)
                sb.append(0);
            sb.append(sTmp.toUpperCase());
        }
        return sb.toString();
    }


}
